import storage from "@/utils/storage.js";

import {
	http,
	Method
} from "@/utils/request.js";



// 数据埋点上报-剧情播放
export const report_storyPlay = ({
	storyId,
	storyName
}) => {
	// let userInfo = storage.getUserInfo()
	// uni.report('storyPlay', {
	// 	'storyId': storyId,
	// 	'storyName': storyName,
	// 	'userId': userInfo.id,
	// 	'username': userInfo.nickName,
	// })
};

// 数据埋点上报-第几秒关闭了充值界面
export const report_closePay = ({
	second
}) => {
	// let userInfo = storage.getUserInfo()
	// uni.report('closePay', {
	// 	'second': second,
	// 	'userId': userInfo.id,
	// 	'username': userInfo.nickName,
	// })
};

// 数据埋点上报-第几秒进行了充值
export const report_goPay = ({
	second
}) => {
	// let userInfo = storage.getUserInfo()
	// uni.report('goPay', {
	// 	'second': second,
	// 	'userId': userInfo.id,
	// 	'username': userInfo.nickName,
	// })
};

// 数据埋点上报-第几秒充值成功
export const report_goPaySuccess = ({
	second
}) => {
	// let userInfo = storage.getUserInfo()
	// uni.report('goPaySuccess', {
	// 	'second': second,
	// 	'userId': userInfo.id,
	// 	'username': userInfo.nickName,
	// })
};

// 数据埋点上报-第几秒充值失败
export const report_goPayFaild = ({
	second
}) => {
	// let userInfo = storage.getUserInfo()
	// uni.report('goPayFaild', {
	// 	'second': second,
	// 	'userId': userInfo.id,
	// 	'username': userInfo.nickName,
	// })
};


/**
 * @ desc 巨量引擎-广告回传上报
 * @param event_type 事件类型
 * @param clickid  归因参数
 * @param selfProperties 自定义属性（有限制，具体可用的参数需要联系官方运营
 * @link https://event-manager.oceanengine.com/docs/8650/app_api_docs
 */
export const ad_report_api_callBack_api = ({
	event_type = 'active',
	selfProperties = {}
}) => {

	storage.get_clickid().then(result => {
		let clickid = result
		// uni.showModal({
		// 	content:`${result}___________${clickid}`
		// })
		if (!clickid) {
			return
		}

		uni.request({
			// url: `https://restapi.amap.com/v3/geocode/regeo`,//第一次打开文档显示的地址
			url: `https://analytics.oceanengine.com/api/v2/conversion`, //第二次打开文档显示的地址。。。
			method: "POST",
			data: {
				"event_type": event_type, //必填，事件类型
				"context": {
					"ad": {
						"callback": clickid, //必填，从启动参数里获取的clickid
						"match_type": 0, // 0：点击归因1：展示归因 2：有效播放归因
					},
					"device": {
						"platform": uni.getSystemInfoSync().platform, //用户设备类型
					}
				},
				"properties": {
					...selfProperties
					// "pay_amount": 49 //用户付款金额
				},
				"timestamp": new Date().getTime()
			},
			success(res) {
				console.log(res, '广告成功回传反馈-----');
				// uni.showModal({
				// 	title: '回传成功',
				// 	content: JSON.stringify(res) + clickid
				// });
			},
			fail(res) {
				console.log(res, '广告失败回传反馈-----');
				// uni.showModal({
				// 	title: '回传失败',
				// 	content: JSON.stringify(res) + clickid
				// });
			}
		})

	})

};

/**
 * @param eventType 上报事件类型
 * login-首次进入小程序时上报
 * startGame-开始游戏时上报 
 * storyPlay-剧情播放结束时上报 
 * startPay-点击支付按钮时上报
 * cancelPay-取消支付时上报 
 * successPay-支付成功时上报
 */
export const self_report_api = (payload = {}) => {
	let launchOption = storage.getLaunchOption()
	let systemInfo = uni.getSystemInfoSync()
	let userInfo = storage.getUserInfo()
	let reportData = {
		gameId: storage.getGameDetail().gameId || '', //游戏id
		gameName: storage.getGameDetail().gameName || '', //游戏名称
		scene: launchOption.scene, //小程序启动场景值
		web_url: launchOption.query.ad_params&&launchOption.query.ad_params.web_url || '', //小程序web地址
		adid: launchOption.query.adid || launchOption.query.ad_params&&launchOption.query.ad_params.ad_id || '', //广告id
		clickid: launchOption.query.clickid || '', //广告点击id
		creativeid: launchOption.query.creativeid || launchOption.query.ad_params&&launchOption.query.ad_params.cid || '', //创意id
		creativetype: launchOption.query.creativetype || '', //创意类型
		name: launchOption.query.name || '', //用户抖音昵称
		age: launchOption.query.age || '', //用户抖音年龄
		deviceId: systemInfo.deviceId, //设备id
		deviceModel: systemInfo.deviceModel, //设备类型
		platform: systemInfo.platform, //操作系统
		system: systemInfo.system, //系统版本
		version: systemInfo.version, //用户抖音版本
		memberId: userInfo.id, //用户登录ID
		lastLoginDate: userInfo.lastLoginDate, //最近一次登录时间
		eventType: '', //埋点事件类型
		storyId: "", //当前剧情id
		storyType: "", //当前剧情类型
		eventStartTime: "", //事件初始化时间
		eventEndTime: "", //事件触发时间
		...payload
	}


	http.request({
		url: "/log/event",
		method: Method.POST,
		needToken: false,
		data: {
			data: reportData
		}
	}).then(res => {
		if (res.data.success) {
			console.log(res, '自定义埋点上报成功')
		} else {
			console.log(res, '自定义埋点上报失败')
		}
	}).catch(error => {
		console.log(res, '自定义埋点上报异常')
	})

}